__author__ = 'Administrator'

from ftplib import FTP
import time

drive = "z:"

ftp_server = '36.110.41.170'
username = "DataSync"
password = "xtcapital_db"

fdatedir=drive + '//data//WindDB//setting//'
#fdatedir=''
tradedatesname='tradingDates.csv'

def getNextDate(cdate):
	reader=open(fdatedir+tradedatesname,'r')
	nextone=0
	ndate=''
	for line in reader:
		if nextone==1 and line[0]!='#':
			ndate=line[0:8]
			break
		if line[0:8]==cdate:
			nextone=1
	return ndate

def getLastDate(cdate):
	reader=open(fdatedir+tradedatesname,'r')
	nextone=0
	ldate=reader.readline()
	next(reader)
	for line in reader:
		if line[0:8]==cdate:
			return ldate
		else:
			ldate=line[0:8]
	return -1

def ftpconnect():
	ftp=FTP()
	ftp.set_debuglevel(2) #打开调试级别2，显示详细信息
	ftp.connect(ftp_server,21) #连接
	ftp.login(username,password) #登录，如果匿名登录则用空串代替即可
	return ftp

def downloadfile(remotefile,localfile):
	ftp = ftpconnect()
	print(ftp.getwelcome()) #显示ftp服务器欢迎信息
	bufsize = 1024 #设置缓冲块大小
	fp = open(localfile,'wb') #以写模式在本地打开文件
	ftp.retrbinary('RETR ' + remotefile,fp.write,bufsize) #接收服务器上文件并写入本地文件
	ftp.set_debuglevel(0) #关闭调试
	fp.close()
	ftp.quit() #退出ftp服务器

def uploadfile(remotefile,localfile):
	ftp = ftpconnect()
	bufsize = 1024
	fp = open(localfile,'rb')
	ftp.storbinary('STOR '+ remotefile ,fp,bufsize) #上传文件
	ftp.set_debuglevel(0)
	fp.close() #关闭文件
	ftp.quit()

def uploadWorkFile(curdate,ftype):
	fpath = drive +"//data//WindDB//" + ftype +  "//"
	fname = curdate +  "." + ftype + ".csv"
	remotepath = "//WindDB//"+ftype+"//"
	floc = fpath + fname
	fremote = remotepath + fname
	uploadfile(fremote,floc)

def downloadWorkFile(curdate,ftype):
	fpath = drive +"//data//WindDB//" + ftype +  "//"
	fname = curdate +  "." + ftype + ".csv"
	remotepath = "//WindDB//"+ftype+"//"
	floc = fpath + fname
	fremote = remotepath + fname
	downloadfile(fremote,floc)

def runUpload(curdate):
	ndate = getNextDate(curdate)
	#dpx
	uploadWorkFile(curdate,"dpx")
	#shares
	uploadWorkFile(curdate,"shares")
	#cacs
	uploadWorkFile(ndate,"cacs")
	#universe
	fremote = "//WindDB//setting//universe//"+ndate + "_universe.csv"
	floc = drive +"//data//WindDB//setting//universe//"+ndate + "_universe.csv"
	uploadfile(fremote,floc)
	# destrade3
	fremote = "//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	floc = drive +"//data//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	uploadfile(fremote,floc)
	# destrade4
	fremote = "//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	floc = drive +"//data//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	uploadfile(fremote,floc)

def runDownload(curdate):
	ndate = getNextDate(curdate)
	#dpx
	downloadWorkFile(curdate,"dpx")
	#shares
	downloadWorkFile(curdate,"shares")
	#cacs
	downloadWorkFile(ndate,"cacs")
	#universe
	fremote = "//WindDB//setting//universe//"+ndate + "_universe.csv"
	floc = drive +"//data//WindDB//setting//universe//"+ndate + "_universe.csv"
	downloadfile(fremote,floc)
	# destrade3
	fremote = "//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	floc = drive +"//data//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	downloadfile(fremote,floc)
	# destrade4
	fremote = "//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	floc = drive +"//data//WindDB//production3//portfolio//desTrade//"+curdate + ".desTrade.csv"
	downloadfile(fremote,floc)


if __name__ == "__main__":
	curdate = time.strftime("%Y%m%d")
	runDownload(curdate)
